﻿using Abp.Domain.Repositories;
using MyCompanyName.AbpZeroTemplate.Prm.Admin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyCompanyName.AbpZeroTemplate.Prm.Project
{
    public class ProjectManager : AbpZeroTemplateDomainServiceBase
    {
        public Abp.Runtime.Session.IAbpSession AbpSession { get; set; }
        public IRepository<ProjectInfo, int> projectRepository { get; private set; }
        public IRepository<Inquiry.Inquiry, int> inquiryRepository { get; private set; }
        public IRepository<ProjectProgress, int> projectProgressRepository { get; private set; }
        public IRepository<ProjectTeam, int> projectTeamRepository { get; private set; }
        public IRepository<ProjectRoleInfo, int> projectRoleInfoRepository { get; private set; }
        public IRepository<TeamComment, int> teamCommentRepository { get; private set; }
        public IRepository<ProjectDocmeut, int> projectDocmeutRepository { get; private set; }
        public IRepository<ProjectDocmeutShareMember, int> projectDocmeutShareMemeberRepository { get; private set; }
        public IRepository<ProjectDocmeutVersion, int> projectDocmeutVersionRepository { get; private set; }
        public IRepository<ProjectDocType, int> projectDocTypeRepository { get; private set; }

        public IRepository<Topic, int> topicRepository { get; private set; }
        public IRepository<Reply, int> replyRepository { get; private set; }

        private readonly IRepository<Product, int> productRepository;


        public ProjectManager(
            IRepository<ProjectInfo, int> _projectRepository,
            IRepository<ProjectProgress, int> _projectProgressRepository,
            IRepository<ProjectTeam, int> _projectTeamRepository,
            IRepository<ProjectRoleInfo, int> _projectRoleInfoRepository,
            IRepository<TeamComment, int> _teamCommentRepository,
            IRepository<ProjectDocmeut, int> _projectDocmeutRepository,
            IRepository<ProjectDocType, int> _projectDocTypeRepository,
            IRepository<ProjectDocmeutVersion, int> _projectDocmeutVersionRepository,
            IRepository<Topic, int> _topicRepositor,
            IRepository<Reply, int> _replyRepository,
             IRepository<ProjectDocmeutShareMember, int> _projectDocmeutShareMemeberRepository,
            IRepository<Inquiry.Inquiry, int> _inquiryRepository,
            IRepository<Product, int> _productRepository
            )
        {
            projectRepository = _projectRepository;
            projectProgressRepository = _projectProgressRepository;
            projectTeamRepository = _projectTeamRepository;
            projectRoleInfoRepository = _projectRoleInfoRepository;
            teamCommentRepository = _teamCommentRepository;
            projectDocmeutRepository = _projectDocmeutRepository;
            projectDocTypeRepository = _projectDocTypeRepository;
            projectDocmeutVersionRepository = _projectDocmeutVersionRepository;
            topicRepository = _topicRepositor;
            projectDocmeutShareMemeberRepository = _projectDocmeutShareMemeberRepository;
            replyRepository = _replyRepository;
            AbpSession = Abp.Runtime.Session.NullAbpSession.Instance;
            inquiryRepository = _inquiryRepository;
            productRepository = _productRepository;
        }



        /// <summary>
        /// 设置项目进度
        /// </summary>
        /// <param name="progress"></param>
        /// <returns></returns>
        public async Task SetProjectProgress(ProjectProgress progress)
        {
            await projectProgressRepository.InsertAndGetIdAsync(progress);
        }


        /// <summary>
        /// 设置项目成员
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="users"></param>
        /// <returns></returns>
        public async Task SetProjectTeam(int projectId, int roleId, List<int> users)
        {
            DateTime dtNow = DateTime.Now;
            DateTime endDate = dtNow.AddYears(5);
            //---重复增加的话
            /*
             * 1、未删除得组员是否存在，没有则直接加入；
             * 2、已经存在了
             * {
             * a\是否
             * }
             * 
             */
            foreach (var item in users)
            {
                var team = projectTeamRepository.GetAll().Where(x => x.ProjectId == projectId && x.ProjectRoleId == roleId && x.UserId == item && !x.IsDeleted).SingleOrDefault();
                if (team == null)
                {
                    var pt = new ProjectTeam() { ProjectId = projectId, ProjectRoleId = roleId, UserId = item, StartTime = dtNow, EndTime = endDate };
                    await projectTeamRepository.InsertAsync(pt);
                }
                else
                {
                    if (team.EndTime.Date < DateTime.Now.Date)
                    {
                        var pt = new ProjectTeam() { ProjectId = projectId, ProjectRoleId = roleId, UserId = item, StartTime = dtNow, EndTime = endDate };
                        await projectTeamRepository.InsertAsync(pt);
                    }
                    else
                    {
                        team.EndTime = endDate;
                        await projectTeamRepository.UpdateAsync(team);
                    }
                }
            }
        }



        public int GetInquiryCount(int projectId)
        {
            var p = projectRepository.Get(projectId);
            var subIds = productRepository.GetAll().Where(x => x.Code == p.ProductNo).Select(x => x.SubstanceId).Distinct().ToList();

            var inquiryCount = inquiryRepository.GetAll().Where(x => subIds.Contains(x.SubId)).Count();
            return inquiryCount;
        }


    }
}
